//
//  ProductDetailViewController.m
//  HAODE
//
//  Created by LiYinDi on 16/1/29.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "ProductDetailViewController.h"
#import "TheLoginViewController.h"
#import "PlaceOrderSpecTableViewCell.h"
#import "HttpRequest.h"
#import "UIImageView+WebCache.h"
#import "SelectedGoodNewViewController.h"
#import "BusinessOrderExtAddViewController.h"
#import "UMSocial.h"
#import "HAODEsingleton.h"
#import "editorProductViewController.h"

#import "BusinessOrderExtAddViewController.h"
#import "BusinessWhsInAddViewController.h"
#import "QRCodeGenerator.h"
#import "BusinessCheckVouchAddViewController.h"
#import "BusinessPOrderAddViewController.h"
#import "BusinessCheckVouchContinueViewController.h"
#import "SpecColorDataModel.h"
#import "SpecSizeDataModel.h"
//#import "Masonry.h"
#import "FMDB.h"
@interface ProductDetailViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate,PlaceOrderSpecCellDelegate,UMSocialUIDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)   NSString*shareUrl;
@property (weak, nonatomic) UIPageControl * pageControl;
@property (nonatomic,retain)UIImageView* image1;
@property(nonatomic,strong)   NSString*shuoming;
@property(nonatomic,strong)FMDatabase *db;
@property(nonatomic,strong)FMDatabase *specdb;
@end

@implementation ProductDetailViewController
{
NSString *colorSpecID;
NSString *sizeSpecID;
NSString *specName;
NSString *specPrice;
NSString *specInv;
NSString *specID;
NSInteger colorIndex;
NSInteger sizeIndex;
NSString *userToken;
 
NSMutableArray *modelArray;
NSMutableArray *titleImageArray;
NSMutableArray *tagNameArray;
UIView*diview;
NSString *itemCodeString;
NSString *itemIDString;
NSString *itemTypeString;

UIView*statusBarView;
UIView*fengxiangview;
UIButton* quxiao;
UIScrollView*fengxiangScroll;
NSMutableArray*guigetitle ;
NSMutableArray*specIdarr ;
NSMutableArray*price ;
NSString*itemCode;
NSMutableArray*specInvarr ;
UIButton*erweima;
UIButton*erweima1;
NSMutableArray*imagearrays;
NSMutableArray*imageUrlarrays;
    
NSString *stockStr;


HAODEsingleton*commdity;
}

-(void)viewWillAppear:(BOOL)animated
{
   
    colorSpecID=@"";
    sizeSpecID=@"";
    specName=@"";
    
    specPrice=@"";
    specInv=@"";
    specID=@"";
 
    imagearrays=[NSMutableArray array];
    imageUrlarrays=[NSMutableArray array];
 
    modelArray=[NSMutableArray array];
    titleImageArray=[NSMutableArray array];
    tagNameArray=[NSMutableArray array];
    commdity=[HAODEsingleton shared];
//    [_totalCountView setBackgroundColor:[Global parseColor:@"#E01F3536"]];
    [self setNavigation];
    [self setAutoLayout];
   
  //  [self setCornerCountLabelText];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _countView.layer.borderWidth=1;
    _countView.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f] CGColor];
    
    _addBtn.layer.borderWidth=1;
    _addBtn.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f] CGColor];
    [self createFB];
    [self createSpecFB];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    userToken = [user objectForKey:@"tokenId"];
//    NSLog(@"userToken>><<<==%@",userToken);
    [self requestProductDetail:_itemIdStr];

}
-(void)createFB
{
    //    NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //
    //    NSString *filename=[doc stringByAppendingPathComponent:@"UserPlaceOrder.sqlite"];
    NSString *filename=[self getDBPath];
//     NSLog(@"filename>><<<==%@",filename);
    FMDatabase *db=[FMDatabase databaseWithPath:filename];
    
    if ([db open]) {
        
        BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_UserPlaceOrder (id integer PRIMARY KEY AUTOINCREMENT , userToken text NOT NULL , itemId text NOT NULL , itemCode text NOT NULL , imageUrlFull text NOT NULL , itemName text NOT NULL , maxPrice text NOT NULL , minPrice text NOT NULL , salePrice text NOT NULL , invQty text NOT NULL , hasSpec text NOT NULL , specCount text NOT NULL , typeString text NOT NULL)"];
        if (result) {
            
        }
        else
        {
            
        }
    }
    self.db=db;
    //    [_db executeUpdate:@"DELETE FROM t_UserPlaceOrder"];
    //    NSLog(@"_dboo===%@",_db);
}
-(void)createSpecFB
{
    //    NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //
    //    NSString *filename=[doc stringByAppendingPathComponent:@"UserSpec.sqlite"];
    //    NSLog(@"filename==%@",filename);
    NSString *filename=[self getDBPath];
    
    FMDatabase *db=[FMDatabase databaseWithPath:filename];
    
    if ([db open]) {
        
        BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_UserSpec (id integer PRIMARY KEY AUTOINCREMENT , userToken text NOT NULL , itemID text NOT NULL , itemCode text NOT NULL , colorSpecID text NOT NULL , sizeSpecID text NOT NULL , specName text NOT NULL , specPrice text NOT NULL , specStock text NOT NULL , specCount text NOT NULL , typeString text NOT NULL , specID text NOT NULL)"];
        if (result) {
            
        }
        else
        {
            
        }
    }
    self.specdb=db;
    //    [_db executeUpdate:@"DELETE FROM t_UserSpec"];
    //    NSLog(@"_dboo===%@",_db);
    
}

-(void)getTotalCountFromNoSpecDB
{
    CGFloat totalNoSpecMoney=0;
    NSInteger count=0;
    //    PlaceOrderSubDataModel *model=modelArray[tag];
    FMResultSet *resultSet=[_db executeQuery:@"SELECT * FROM t_UserPlaceOrder WHERE userToken = ? AND typeString = ? AND itemId = ?;" ,userToken,self.typeString,self.itemIdStr];
    
    while (resultSet.next) {
        NSString *countString=[resultSet objectForColumnName:@"specCount"];
        count=count+[countString integerValue];
        
//        NSString *countString=[resultSet objectForColumnName:@"specCount"];
        NSString *salePriceString=[resultSet objectForColumnName:@"salePrice"];
        totalNoSpecMoney=totalNoSpecMoney+[countString integerValue]*[salePriceString floatValue];
        
    }
    [resultSet close];
    _cornerLabel.text=[NSString stringWithFormat:@"%ld",(long)count];
    _totalLabel.text=[NSString stringWithFormat:@"%0.02f",totalNoSpecMoney];
    
}
-(void)getTotalCountFromHasSpecDB
{
     CGFloat totalHasSpecMoney=0;
    NSInteger count=0;
    //    PlaceOrderSubDataModel *model=modelArray[tag];
    FMResultSet *resultSet=[_db executeQuery:@"SELECT * FROM t_UserSpec WHERE userToken = ? AND typeString = ? AND itemID = ?;" ,userToken,self.typeString,self.itemIdStr];
    
    while (resultSet.next) {
        NSString *countString=[resultSet objectForColumnName:@"specCount"];
        count=count+[countString integerValue];
        
//        NSString *countString=[resultSet objectForColumnName:@"specCount"];
        
        NSString *salePriceString=[resultSet objectForColumnName:@"specPrice"];
        
        totalHasSpecMoney=totalHasSpecMoney+[countString integerValue]*[salePriceString floatValue];
    }
    [resultSet close];
    _cornerLabel.text=[NSString stringWithFormat:@"%ld",(long)count];
     _totalLabel.text=[NSString stringWithFormat:@"%0.02f",totalHasSpecMoney];
}


- (IBAction)subtractCountBtnClick:(id)sender {
    
    NSString *countStr=_countTextField.text;
    countStr=[NSString stringWithFormat:@"%d",[countStr intValue]-1];
    if ([countStr intValue]>=1) {
        _countTextField.text=countStr;
    }
}

- (IBAction)addCountBtnClick:(id)sender {
    _countTextField.text=[NSString stringWithFormat:@"%d",[_countTextField.text intValue]+1];
}
- (IBAction)addBtnClick:(id)sender {
    //先在表中查询有没有相同的元素，如果有，做修改操作
    
//    if ([_typeString isEqualToString:@"WhsOut"]||[_typeString isEqualToString:@"PlaceOrder"]||[_typeString isEqualToString:@"Exhibition"]) {
    if ([_typeString isEqualToString:@"WhsOut"]||[_typeString isEqualToString:@"PlaceOrder"]) {
        FMResultSet *rs=[_db executeQuery:@"SELECT * from t_UserSpec WHERE userToken = ? AND itemID = ? AND colorSpecID = ? AND sizeSpecID = ? AND typeString = ?;",userToken, self.itemIdStr,colorSpecID,sizeSpecID,self.typeString];
        //     NSLog(@"rs..==%@",rs);
        if([rs next])
            
        {
            NSInteger idINDEX = [rs objectForColumnName:@"id"];
            NSString * count = [rs objectForColumnName:@"specCount"];
            NSString * inv = [rs objectForColumnName:@"specStock"];
            // NSLog(@"invqq===%@",inv);
            
            NSString *nCount=[NSString stringWithFormat:@"%d",[count intValue]+[_countTextField.text intValue]];
            //            NSLog(@"nCountqq===%@",nCount);
            if ([nCount intValue]<=[inv intValue]) {
                [_db executeUpdate:@"UPDATE t_UserSpec SET specCount = ?  WHERE id = ?;",nCount,idINDEX];
                _cornerLabel.text=[NSString stringWithFormat:@"%d",[_cornerLabel.text intValue]+[_countTextField.text intValue]];
//                _countLabel.hidden=NO;
              
                [Global showTextHudSub:@"添加成功" view:self.view];
            }
            else
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"已超商品库存量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        }
        //向数据库中插入一条数据
        
        else{
            //            NSLog(@"[specInv intValue]===%d",[specInv intValue]);
            if ([_countTextField.text intValue]<=[specInv intValue]) {
                
                
                [_db executeUpdate:@"INSERT INTO t_UserSpec (userToken,itemID,itemCode,colorSpecID,sizeSpecID,specName,specPrice,specStock,specCount,typeString,specID) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",userToken,self.itemIdStr,_itemCodeStr,colorSpecID,sizeSpecID,specName,specPrice,specInv,_countTextField.text,_typeString,specID];
                _cornerLabel.text=[NSString stringWithFormat:@"%d",[_cornerLabel.text intValue]+[_countTextField.text intValue]];
//                _countLabel.hidden=NO;
//                _countBackGroundLabel.hidden=NO;
//                _xLabel.hidden=NO;
                
                [Global showTextHudSub:@"添加成功" view:self.view];
            }
            else
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"已超商品库存量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        }
        
        [rs close];
        
    }
    else
    {
        FMResultSet *rs=[_db executeQuery:@"SELECT * from t_UserSpec WHERE userToken = ? AND itemID = ? AND colorSpecID = ? AND sizeSpecID = ? AND typeString = ?;",userToken, self.itemIdStr,colorSpecID,sizeSpecID,_typeString];
        //     NSLog(@"rs..==%@",rs);
        if([rs next])
            
        {
            NSInteger idINDEX = [rs objectForColumnName:@"id"];
            NSString * count = [rs objectForColumnName:@"specCount"];
            
            NSString *nCount=[NSString stringWithFormat:@"%d",[count intValue]+[_countTextField.text intValue]];
            
            [_db executeUpdate:@"UPDATE t_UserSpec SET specCount = ?  WHERE id = ?;",nCount,idINDEX];
            _cornerLabel.text=[NSString stringWithFormat:@"%d",[_cornerLabel.text intValue]+[_countTextField.text intValue]];
//            _countLabel.hidden=NO;
//            _countBackGroundLabel.hidden=NO;
//            _xLabel.hidden=NO;
        }
        //向数据库中插入一条数据
        
        else{
            
            [_db executeUpdate:@"INSERT INTO t_UserSpec (userToken,itemID,itemCode,colorSpecID,sizeSpecID,specName,specPrice,specStock,specCount,typeString,specID) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",userToken,self.itemIdStr,_itemCodeStr,colorSpecID,sizeSpecID,specName,specPrice,specInv,_countTextField.text,_typeString,specID];
            _cornerLabel.text=[NSString stringWithFormat:@"%d",[_cornerLabel.text intValue]+[_countTextField.text intValue]];
//            _countLabel.hidden=NO;
//            _countBackGroundLabel.hidden=NO;
//            _xLabel.hidden=NO;
            
        }
        
        [rs close];
        [Global showTextHudSub:@"添加成功" view:self.view];
        
    }
    
}

-(void)requestProductDetail:(NSString *)codeStr
{
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    
    [param setValue:codeStr forKey:@"itemId"];
    [param setValue:userToken forKey:@"tokenId"];
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    
//     NSLog(@"ddddsss===%@",param);
    [HttpRequest requestData:@"Product/GetProductDetail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
//     NSLog(@"dddd===%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                commdity.Dict=severDataDict.dataDict;
                commdity.itemId=codeStr;
                commdity.content=[severDataDict.dataDict objectForKey:@"itemType"];
                commdity.code=[severDataDict.dataDict objectForKey:@"itemTypeCode"];
                commdity.DataLine=[severDataDict.dataDict objectForKey:@"tagList"];
                commdity.specListarr=[severDataDict.dataDict objectForKey:@"specList"];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                arrr=[severDataDict.dataDict objectForKey:@"tagList"];
                
                
                NSMutableArray* Codearrr=[[NSMutableArray alloc]init];
                NSMutableArray* Namearrr=[[NSMutableArray alloc]init];
                for (NSDictionary* dicc in arrr )
                {
                    [Codearrr addObject:[dicc valueForKey:@"code"]];
                    [Namearrr addObject:[dicc valueForKey:@"content"]];
                    
                    
                }
                
                commdity.selectTagNameArray=[Namearrr copy];
                commdity.selectTagCodeArray=[Codearrr copy];
                
                
                
                for (NSDictionary *dic in [severDataDict.dataDict valueForKey:@"imageList"]) {
                   
                    [self getImageFromURL:[dic objectForKey:@"imageUrlFull"]];
                    [self getImageFromURLs:[dic objectForKey:@"imageUrl"]];
                }
 
                
                
                NSMutableArray *titleImageListArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"imageList"]];
                [titleImageArray removeAllObjects];
                for (int i=0;i<titleImageListArray.count;i++)
                {
                    [titleImageArray addObject:[[titleImageListArray objectAtIndex: i] valueForKey:@"imageUrlFull"]];
                }
               
                [self setImageScrollView];
                _itemCodeStr=[severDataDict.dataDict objectForKey:@"itemCode"];
                itemTypeString=[severDataDict.dataDict objectForKey:@"itemType"];
               
                _barcodeNumLabel.text=_itemCodeStr;
                _stockLabel.text=[NSString stringWithFormat:@"库存%ld件",[[severDataDict.dataDict objectForKey:@"invQty"] integerValue]];
                stockStr=[NSString stringWithFormat:@"%ld",[[severDataDict.dataDict objectForKey:@"invQty"] integerValue]];
 
                _productNameLabel.text=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"itemName"]];
                 _detailLabel.text=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"details"]];
                
                NSString *minPriceStr=[NSString stringWithFormat:@"%0.02f",[[severDataDict.dataDict objectForKey:@"minPrice"] floatValue]];
                NSString *maxPriceStr=[NSString stringWithFormat:@"%0.02f",[[severDataDict.dataDict objectForKey:@"maxPrice"] floatValue]];
                if ([minPriceStr floatValue]==[maxPriceStr floatValue]) {
                    _priceLabel.text=[NSString stringWithFormat:@"¥%@",[Global floatConvertPercentile:[minPriceStr floatValue]]];
                }
                else
                {
                    _priceLabel.text=[NSString stringWithFormat:@"¥%@~%@",[Global floatConvertPercentile:[minPriceStr floatValue]],[Global floatConvertPercentile:[maxPriceStr floatValue]]];
                }
                
                 NSMutableArray *tagArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"tagList"]];
                [tagNameArray removeAllObjects];
                for (UIView *v in _tagView.subviews) {
                    [v removeFromSuperview];
                }
                for (int i=0;i<tagArray.count;i++)
                {
                    [tagNameArray addObject:[[tagArray objectAtIndex: i] valueForKey:@"content"]];
                }
               
                [self setTagViewWithArray:tagNameArray];
                NSArray *dataArray=severDataDict.dataDict[@"itemColours"];
                for (NSDictionary *dic in dataArray) {
                    SpecColorDataModel *dataModel=[SpecColorDataModel dataModelWithDict:dic];
                    [modelArray addObject:dataModel];
                    
                }
                
                if ([[severDataDict.dataDict objectForKey:@"specType"] intValue]==0) {
                    [self getTotalCountFromNoSpecDB];
                    _priceLineLabel.hidden=YES;
                }
                else
                {
                     _priceLineLabel.hidden=NO;
                    [self getTotalCountFromHasSpecDB];
                     [self setColor];
                    
                }

                
             }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];

}
-(void)setColor
{
    for (UIView *v in _colorView.subviews ) {
        [v removeFromSuperview];
    }
    
    UILabel *colorTag=[[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 14)];
    colorTag.text=@"颜色";
    colorTag.tag=1000;
    [colorTag setFont:[UIFont systemFontOfSize:14]];
    colorTag.textColor=[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0f];
    [_colorView addSubview:colorTag];
    
    UIImageView *lineImageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 44,ScreenWidth-20 , 1)];
    lineImageView.tag=1000;
    [lineImageView setImage:[UIImage imageNamed:@"hengxian.png"] ];
    [_colorView addSubview:lineImageView];
    
    for (int i=0; i<modelArray.count; i++) {
        SpecColorDataModel *dataModel=modelArray[i];
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        CGRect rect=button.frame;
        rect.size.height=33;
        rect.size.width=(ScreenWidth-48)/3;
        rect.origin.x=13+((ScreenWidth-48)/3)*(i%3)+(i%3)*11;
        rect.origin.y=56+40*(i/3);
        button.frame=rect;
        button.tag=i;
        [button setBackgroundColor:[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0f]];
        
        
        button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
        [button setTitle:dataModel.colourName forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        
        if (i==0) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0]];
            colorSpecID=dataModel.colourId;
            specName=dataModel.colourName;
            specInv=dataModel.specInv;
            specPrice=dataModel.specPrice;
            
            specID=dataModel.specId;
            NSString *str=[NSString stringWithFormat:@"库存 %@",specInv];
            NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
            [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0] range:NSMakeRange(0, 2)];
            
            _stockLabel.attributedText=attriString;
            
            
        }
        
        [button addTarget:self action:@selector(colorButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_colorView addSubview:button];
        
        
        if (i==modelArray.count-1) {
            
            CGFloat height=button.frame.origin.y+button.frame.size.height+11;
            
            
            [_colorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_priceLineLabel.mas_bottom);
                make.bottom.equalTo(_priceLineLabel.mas_bottom).with.offset(height);
                make.left.equalTo(_scrollContentView.mas_left);
                make.right.equalTo(_scrollContentView.mas_right);
                
            }];
            
            [self setSize:0];
        }
        
        
    }
}
-(void)colorButtonClick:(UIButton *)sender
{
    for (UIButton *btn in _colorView.subviews ) {
        if (btn.tag!=1000) {
            
            
            [btn setBackgroundColor:[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0]];
            [btn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        }
    }
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sender setBackgroundColor:[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0]];
    SpecColorDataModel *colorModel=modelArray[sender.tag];
    colorIndex=sender.tag;
    colorSpecID=colorModel.colourId;
    specName=colorModel.colourName;
    specID=colorModel.specId;
    specInv=colorModel.specInv;
    //    NSLog(@"colorModel.specInv==%@",colorModel.specInv);
    //    NSLog(@"specInv pp==%@",specInv);
    if (colorModel.sizeModelArray.count>0) {
        //       NSLog(@"olorModel.sizeModelArraypp==%@",colorModel.sizeModelArray);
        SpecSizeDataModel *sizeModel=colorModel.sizeModelArray[0];
        sizeSpecID=sizeModel.sizeId;
        specName=[NSString stringWithFormat:@"%@%@",specName,sizeModel.sizeName];
        specInv=sizeModel.specInv;
        specPrice=sizeModel.specPrice;
        specID=sizeModel.specId;
        [self setSize:sender.tag];
    }
    
    
    NSString *str=[NSString stringWithFormat:@"库存 %@",specInv];
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
    [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0] range:NSMakeRange(0, 2)];
    
    _stockLabel.attributedText=attriString;
    
    
}

-(void)setSize:(NSInteger)senderTag
{
    for (UIView *v in _sizeView.subviews ) {
        [v removeFromSuperview];
    }
    SpecColorDataModel *colorModel=modelArray[senderTag];
    
    NSMutableArray   *sizeModelArray=colorModel.sizeModelArray ;
    
    //    NSLog(@"sizeModelArraymmmmm====%@",sizeModelArray);
    if (sizeModelArray.count>0) {
        
        
        UILabel *colorTag=[[UILabel alloc] initWithFrame:CGRectMake(16, 0+24, 100, 14)];
        colorTag.tag=1000;
        colorTag.text=@"尺码";
        [colorTag setFont:[UIFont systemFontOfSize:14]];
        colorTag.textColor=[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0f];
        [_sizeView addSubview:colorTag];
        
        UIImageView *lineImageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 24+24, ScreenWidth-20, 1)];
        lineImageView.tag=1000;
        [lineImageView setImage:[UIImage imageNamed:@"hengxian.png"] ];
        [_sizeView addSubview:lineImageView];
        
        
        for (int i=0; i<sizeModelArray.count; i++) {
            SpecSizeDataModel *sizeModel=sizeModelArray[i];
            
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            CGRect rect=button.frame;
            rect.size.height=32;
            
            rect.size.width=(ScreenWidth-59)/4;
            rect.origin.x=13+((ScreenWidth-59)/4)*(i%4)+(i%4)*10;
            //        rect.size.width=ScreenWidth/4.5;
            //        rect.origin.x=10+(ScreenWidth/4.2)*(i%4);
            rect.origin.y=36+24+40*(i/4);
            button.frame=rect;
            button.tag=i;
            [button setBackgroundColor:[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0f]];
            
            
            button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
            [button setTitle:sizeModel.sizeName forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(sizeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_sizeView addSubview:button];
            
            if (i==0) {
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [button setBackgroundColor:[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0]];
                sizeSpecID=sizeModel.sizeId;
                specName=[NSString stringWithFormat:@"%@%@",colorModel.colourName,sizeModel.sizeName];
                specInv=sizeModel.specInv;
                specPrice=sizeModel.specPrice;
                if (colorModel.specId.length>0 ) {
                    specID=colorModel.specId;
                }
                else
                {
                    specID=sizeModel.specId;
                }
                
                
                
                NSString *str=[NSString stringWithFormat:@"库存 %@",specInv];
                NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
                [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0] range:NSMakeRange(0, 2)];
                
                _stockLabel.attributedText=attriString;
                
            }
            
            if (i==sizeModelArray.count-1) {
                
                CGFloat height=button.frame.origin.y+button.frame.size.height+11;
                [_sizeView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(_colorView.mas_bottom);
                    make.bottom.equalTo(_colorView.mas_bottom).with.offset(height);
                    make.left.equalTo(_scrollContentView.mas_left);
                    make.right.equalTo(_scrollContentView.mas_right);
                    
                }];
                
            }
            
            
        }
    }
    
}
-(void)sizeButtonClick:(UIButton *)sender
{
    sizeIndex=sender.tag;
    for (UIButton *btn in _sizeView.subviews ) {
        if (btn.tag!=1000) {
            [btn setBackgroundColor:[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0f]];
            [btn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        }
    }
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sender setBackgroundColor:[UIColor colorWithRed:90.0f/255.0f green:130.0f/255.0f blue:26.0f/255.0f alpha:1.0]];
    SpecColorDataModel *colorModel=modelArray[colorIndex];
//    NSLog(@"colorModel.sizeModelArray==%@",colorModel.sizeModelArray);
    SpecSizeDataModel *sizeModel=colorModel.sizeModelArray[sender.tag];
    
    sizeSpecID=sizeModel.sizeId;
    specName=[NSString stringWithFormat:@"%@%@",colorModel.colourName,sizeModel.sizeName];
    specInv=sizeModel.specInv;
    specPrice=sizeModel.specPrice;
    if (colorModel.specId.length>0 ) {
        specID=colorModel.specId;
    }
    else
    {
        specID=sizeModel.specId;
    }
    
    NSString *str=[NSString stringWithFormat:@"库存 %@",specInv];
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:str];
    [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0] range:NSMakeRange(0, 2)];
    
    _stockLabel.attributedText=attriString;
}

-(void)setTagViewWithArray:(NSMutableArray *)array
{
    
        for (int i=0; i<array.count; i++) {
            
            
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            CGRect rect=button.frame;
            rect.size.height=32;
            rect.size.width=ScreenWidth/4.5;
            rect.origin.x=(ScreenWidth/4.2)*(i%4);
            rect.origin.y=12+40*(i/4);
            button.frame=rect;
            button.tag=i;
            [button setBackgroundColor:[UIColor colorWithRed:254.0/255.0 green:249.0/255.0 blue:229.0f/255.0f alpha:1.0]];
            button.layer.borderWidth=1;
            button.layer.borderColor=[[UIColor colorWithRed:213.0f/255.0f green:180.0f/255.0f blue:102.0f/255.0f alpha:1.0f] CGColor];
            button.layer.cornerRadius=16;
            button.titleLabel.font = [UIFont systemFontOfSize: 15.0];
            [button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithRed:213.0f/255.0f green:180.0f/255.0f blue:102.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
            [_tagView addSubview:button];
            if (i==array.count-1) {
                [_tagView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(_scrollContentView.mas_left).with.offset(10);
                    make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
                    make.top.equalTo(_priceLabel.mas_bottom);
                    make.bottom.equalTo(_priceLabel.mas_bottom).with.offset(button.frame.origin.y+button.frame.size.height);
                    
                }];

            }
            }
    
   
}


-(NSString *) getImageFromURLs:(NSString *)imageUrl {
    
    
    
    NSString * results;
    
    
    [imageUrlarrays addObject:imageUrl];
    
    
    commdity.imageUrls=imageUrlarrays;
    return results;
    
}

-(UIImage *) getImageFromURL:(NSString *)fileURL {
    
    
    UIImage * result;
    
    
    
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    
    result = [UIImage imageWithData:data];
    
    [imagearrays addObject:result];
   // NSLog(@"aaaaaaaa%@",imagearrays);
    
    commdity.imageUrlFullarr=imagearrays;
    return result;
    
}

-(void)setImageScrollView
{
    for (UIView *v in _titleImageScrollView.subviews) {
        [v removeFromSuperview];
    }
    
    [_titleImageScrollView setContentSize:CGSizeMake(ScreenWidth * titleImageArray.count, 0)];
    
    
    [_titleImageScrollView setPagingEnabled:YES];
    
    
    [_titleImageScrollView setShowsVerticalScrollIndicator:NO];
    [_titleImageScrollView setShowsHorizontalScrollIndicator:NO];
    
    for (int i =0; i<titleImageArray.count; i++)
    {
        UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(i * ScreenWidth,0,ScreenWidth,ScreenWidth)] ;
        [imageView setImageWithURL:[NSURL URLWithString:[titleImageArray objectAtIndex:i] ]];
        
        [_titleImageScrollView addSubview:imageView];
        
    }
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView==_titleImageScrollView) {
        
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        [_titleImageScrollView setContentOffset:CGPointMake(pageWidth*page, 0)];
        
        // NSLog(@"page///====%d",page);
    }
    
}

- (IBAction)selectedGoodBtnClick:(id)sender {
    
    
    
//    SelectedGoodNewViewController *selectedGoodVC=[[SelectedGoodNewViewController alloc] init];//2.0{9个参数 少2个}
//    selectedGoodVC.typeString=@"Exhibition";
////    selectedGoodVC.saveTypeStr=_saveTypeStr;
////   
////     selectedGoodVC.Whs=self.Whs;
////    //下面继续盘点数据
//    selectedGoodVC.ckvId=self.ckvId;
//    selectedGoodVC.ckvNo=self.ckvNo;
//    selectedGoodVC.whsName=self.whsName;
//    selectedGoodVC.itemTypeName=self.itemTypeName;
//    selectedGoodVC.whsId=self.whsId;
//    selectedGoodVC.itemTypeCode=self.itemTypeCode;
////
//    [self.navigationController pushViewController:selectedGoodVC animated:YES];
}


-(void)setAutoLayout
{
   
    
    _cornerLabel.layer.cornerRadius=10;
    _cornerLabel.layer.masksToBounds=YES;
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top) ;
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(_lastView.mas_top);
    }];
    [_scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView);
        make.width.equalTo(_scrollView);
    }];
    [_titleImageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left);
        make.right.equalTo(_scrollContentView.mas_right);
        make.top.equalTo(_scrollContentView.mas_top);
         make.bottom.equalTo(_scrollContentView.mas_top).with.offset(ScreenWidth);
    }];
    [_barcodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(11);
        make.height.equalTo(@30);
        make.width.equalTo(@30);
        make.top.equalTo(_titleImageScrollView.mas_bottom).with.offset(8);
    }];
    [_barcodeNumTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_barcodeBtn.mas_right).with.offset(3);
        make.height.equalTo(@30);
        make.width.equalTo(@19);
        make.centerY.equalTo(_barcodeBtn.mas_centerY) ;
    }];
    [_barcodeNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_barcodeNumTag.mas_right);
        make.height.equalTo(@30);
        make.right.equalTo(_stockLabel.mas_left);
        make.centerY.equalTo(_barcodeBtn.mas_centerY) ;
    }];
    [_stockLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(-16);
        make.height.equalTo(@30);
       
        make.centerY.equalTo(_barcodeBtn.mas_centerY) ;
    }];
    [_productNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(16);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_barcodeBtn.mas_bottom).with.offset(8);
         make.height.equalTo(@21);
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.equalTo(_scrollContentView.mas_left).with.offset(16);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_productNameLabel.mas_bottom).with.offset(15);
        make.height.equalTo(@21);
    }];
    
    [_tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(10);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_priceLabel.mas_bottom);
        make.height.equalTo(@1);

    }];
    [_priceLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_barcodeBtn.mas_left);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_tagView.mas_bottom).with.offset(10);
        make.height.equalTo(@1);
    }];
    

    [_colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0);
        make.left.equalTo(_scrollContentView.mas_left);
        make.right.equalTo(_scrollContentView.mas_right);
        make.top.equalTo(_priceLineLabel.mas_bottom).with.offset(0);
    }];
    [_sizeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0);
        make.left.equalTo(_scrollContentView.mas_left);
        make.right.equalTo(_scrollContentView.mas_right);
        make.top.equalTo(_colorView.mas_bottom);
    }];
    [_countParentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@67);
        make.left.equalTo(_scrollContentView.mas_left);
        make.right.equalTo(_scrollContentView.mas_right);
        make.top.equalTo(_sizeView.mas_bottom);
        make.bottom.equalTo(_detailTagLineLabel.mas_top);
    }];
    [_countParentTopLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countParentView.mas_top) ;
        
        make.right.equalTo(_countParentView.mas_right).with.offset(-10);
        make.height.equalTo(@1);
        make.left.equalTo(_countParentView.mas_left).with.offset(10);
        
    }];
    [_countParentBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_countParentView.mas_bottom) ;
        
        make.right.equalTo(_countParentView.mas_right).with.offset(-10);
        make.height.equalTo(@1);
        make.left.equalTo(_countParentView.mas_left).with.offset(10);
        
    }];
    
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countParentView.mas_top).with.offset(16.5);
        
        make.right.equalTo(_countParentView.mas_right).with.offset(-10);
        make.height.equalTo(@36);
        make.width.equalTo(@90);
        
    }];
    
    [_countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_countParentView.mas_left).with.offset(50);
        
        make.right.equalTo(_addBtn.mas_left).with.offset(-15);
        make.height.equalTo(@34);
        make.centerY.equalTo(_countParentView.mas_centerY);
        
    }];
    [_subtractCountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countView.mas_top) ;
        make.bottom.equalTo(_countView.mas_bottom) ;
        make.left.equalTo(_countView.mas_left) ;
        
        make.width.equalTo(@34);
        
    }];
    [_addCountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countView.mas_top) ;
        make.bottom.equalTo(_countView.mas_bottom) ;
        make.right.equalTo(_countView.mas_right) ;
        
        make.width.equalTo(@34);
        
    }];
    
    [_countTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countView.mas_top) ;
        make.bottom.equalTo(_countView.mas_bottom) ;
        make.right.equalTo(_addCountBtn.mas_left) ;
        
        make.left.equalTo(_subtractCountBtn.mas_right) ;
        
    }];

    [_detailTagLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(10);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_countParentView.mas_bottom).with.offset(10);
        make.height.equalTo(@1);
    }];

    [_detailTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(16);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_detailTagLineLabel.mas_bottom).with.offset(8);
        make.height.equalTo(@21);
    }];
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollContentView.mas_left).with.offset(10);
        make.right.equalTo(_scrollContentView.mas_right).with.offset(-10);
        make.top.equalTo(_detailTag.mas_bottom).with.offset(3);
        make.bottom.equalTo(_scrollContentView.mas_bottom);
    }];
    [_scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_detailLabel.mas_bottom);}];

    [_lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left) ;
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@50) ;
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    [_submitConfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_lastView.mas_right) ;
        make.width.equalTo(@140) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_lastView.mas_bottom);
    }];
    [_totalCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lastView.mas_left) ;
        make.right.equalTo(_submitConfirmBtn.mas_left) ;
        make.height.equalTo(@50) ;
        make.bottom.equalTo(_lastView.mas_bottom);
    }];

 
}
//-(void)initDataBase
//{
//    NSString *dbPath = [self getDBPath];
//    NSLog(@"dbPath===%@",dbPath);
//    _db = [FMDatabase databaseWithPath:dbPath];
//    if (![_db open]) {
//        NSLog(@"open membersDB failed");
//        return;
//    }
//    
//}
-(NSString *)getDBPath
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *DBPath = [documentPath stringByAppendingPathComponent:kUserDBNAME];
    return DBPath;
}

-(void)setNavigation
{
     self.navigationController.navigationBarHidden=YES;
//    UIImage* backImage = [UIImage imageNamed:@"back.png"];
//    CGRect backframe = CGRectMake(0,0,34/3,62/3);
//    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
//    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
//    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
//    [backButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
////    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
//    self.navigationController.navigationBarHidden=NO;
//    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//    self.navigationController.interactivePopGestureRecognizer.delegate = self;
//    
//    UIButton*shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0.0, 26, 26)];
//    [shareBtn setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
//    [shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIButton*edit = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 30, 30)];
//    [edit setImage:[UIImage imageNamed:@"bianji"] forState:UIControlStateNormal];
//    [edit addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
//    UIBarButtonItem* myBtnBtn1 = [[UIBarButtonItem alloc] initWithCustomView:edit];
//    
//    NSArray *buttonArray = [[NSArray alloc]initWithObjects:myBtnBtn1,myBtnBtn, nil];
//    self.navigationItem.rightBarButtonItems = buttonArray;
//     self.navigationController.navigationBar.barStyle =UIBaselineAdjustmentNone ;
//    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
//    [lineLabel setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0]];
//    [self.navigationController.navigationBar addSubview:lineLabel];
   
    

}
-(IBAction)BackAction:(id)sender
{
     [self.navigationController popViewControllerAnimated:YES];
}

-(void)editBtnClick
{
    editorProductViewController *detailViewController = [[editorProductViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
}
- (IBAction)barCodeBtnClick:(id)sender {
    
    
   
        diview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        diview.backgroundColor=[UIColor blackColor];
        diview.alpha=0.68;
        [self.view addSubview:diview];
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
        singleFingerOne.numberOfTouchesRequired = 1; //手指数
        singleFingerOne.numberOfTapsRequired = 1; //tap次数
        singleFingerOne.delegate = self;
        [diview addGestureRecognizer:singleFingerOne];
        erweima=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2,  ScreenWidth/2)];
        erweima.center=diview.center;
        
        erweima.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:erweima];
        
        erweima1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/3*1.5,  ScreenWidth/3*1.5)];
        erweima1.center=diview.center;
        
        [erweima1 setImage:[QRCodeGenerator qrImageForString:_itemIdStr imageSize:erweima1.bounds.size.width] forState:UIControlStateNormal];
        [self.view addSubview:erweima1];
    

}
- (IBAction)confirmSubmit:(id)sender
{
   // NSLog(@">>===%@",_saveTypeStr);
    float totalValue=0.0;
    NSMutableArray *imgArray=[NSMutableArray array];
    NSMutableArray *nameArray=[NSMutableArray array];
    NSMutableArray *salePriceArray=[NSMutableArray array];
    NSMutableArray *countArray=[NSMutableArray array];
    NSMutableArray *idArray=[NSMutableArray array];
    NSMutableArray *specName=[NSMutableArray array];
    NSMutableArray *specID=[NSMutableArray array];
    
    NSMutableArray *itemCodeArray=[NSMutableArray array];
    NSMutableArray *itemTypeArray=[NSMutableArray array];
    //NSMutableArray *allProductCountArray=[NSMutableArray array];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    // NSLog(@"rrr====%@",[userDefaults objectForKey:@"PLACEORDERDATA"]);
    NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithDictionary:[userDefaults objectForKey:_saveTypeStr]];
    NSArray *keyArray= [dict allKeys];
    
    for (int i=0; i<keyArray.count; i++) {
        NSString *itemCodeStr=[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemCode"];
        
        [itemCodeArray addObject:itemCodeStr];
        [nameArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemName"]];
        NSString *itemTypeStr=[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemType"];
        
        [itemTypeArray addObject:itemTypeStr];
        [imgArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemUrl"]];
        [idArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemID"]];
        //          [productNameArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemCode"]];
        [salePriceArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemPrice"]];
        [countArray addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemCount"]];
        [specName addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemSpecName"]];
        [specID addObject:[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemSpecID"]];
        
        NSString *countStr=[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemCount"];
        NSString *priceStr=[[dict objectForKey:[keyArray objectAtIndex:i] ] objectForKey:@"itemPrice"];
        
        totalValue=totalValue+[countStr intValue]*[priceStr floatValue];
        
    }
    
    if ([stockStr intValue]==0) {
        
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"商品库存为0" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        
    }else{
    
         if ([commdity.gengxin isEqualToString:@""]) {//直接下单
            
            BusinessOrderExtAddViewController *shouyinVC=[[BusinessOrderExtAddViewController alloc] init];//2.0{9个参数 少2个}
            shouyinVC.headImageFullArray=imgArray;
            shouyinVC.itemCodeArray=itemCodeArray;
            shouyinVC.specNameArray=specName;
            shouyinVC.itemIdArray= idArray;
            shouyinVC.itemNameArray=nameArray;
            shouyinVC.specIdArray=specID;
            shouyinVC.dealPriceArray=salePriceArray;
            shouyinVC.quantityArray=countArray;
            shouyinVC.saveTypeStr=_saveTypeStr;
            shouyinVC.TotalMoney=[NSString stringWithFormat:@"%0.02f",totalValue];
            [self.navigationController pushViewController:shouyinVC animated:YES];

           
            
            
        }else{
            
            //陈列入库出库盘点下单
            commdity.whsName=@"";
            commdity.content=@"";
            commdity.leixin=@"";
            commdity.code=@"";
            commdity.ItemCode44= itemCodeArray;
            commdity.Itemid44= idArray;
            commdity.ItemName44= nameArray;
            commdity.ImageUrl44= imgArray;
            commdity.Price44= salePriceArray;
            commdity.currentSpecId44=specID;
            commdity.how44=countArray;
            commdity.specName44=specName;
//            NSLog(@"self.Whs%@",self.Whs);
            
            if ([self.typeString isEqualToString:@"Shelf"]) {
                
                BusinessWhsInAddViewController *detailViewController = [[BusinessWhsInAddViewController alloc] init];
                detailViewController.titleName=@"确认陈列";
                detailViewController.Whs=@"Shelf";
                detailViewController.saveTypeStr=_saveTypeStr;
                
                [self.navigationController pushViewController:detailViewController animated:YES];
                
                
            }
            
            if ([self.typeString isEqualToString:@"WhsOut"]) {
                BusinessWhsInAddViewController *detailViewController = [[BusinessWhsInAddViewController alloc] init];
                detailViewController.titleName=@"确认出库";
                detailViewController.Whs=@"WhsOut";
                detailViewController.Type=@"WhsOutType";
                detailViewController.saveTypeStr=_saveTypeStr;
                [self.navigationController pushViewController:detailViewController animated:YES];
                
                
            }
            
            if ([self.typeString isEqualToString:@"WhsIn"]) {
                BusinessWhsInAddViewController *detailViewController = [[BusinessWhsInAddViewController alloc] init];
                detailViewController.titleName=@"确认入库";
                detailViewController.Whs=@"WhsIn";
                detailViewController.Type=@"WhsInType";
                detailViewController.saveTypeStr=_saveTypeStr;
                [self.navigationController pushViewController:detailViewController animated:YES];
                
            }
            
            
            if ([self.typeString isEqualToString:@"CheckVouch"]) {
                
                BusinessCheckVouchAddViewController *detailViewController = [[BusinessCheckVouchAddViewController alloc] init];
                detailViewController.titleName=@"确认录入";
                detailViewController.Whs=@"CheckVouch";
                //            commdity.content=@"";
                commdity.content=itemTypeArray[0];
                commdity.content1=@"";
                detailViewController.saveTypeStr=_saveTypeStr;
                [self.navigationController pushViewController:detailViewController animated:YES];
            }
            
            if ([self.typeString isEqualToString:@"POrder"]) {
                
                BusinessPOrderAddViewController *detailViewController = [[BusinessPOrderAddViewController alloc] init];
                detailViewController.titleName=@"采购订单";
                detailViewController.Whs=@"POrder";
                commdity.content=@"";
                detailViewController.saveTypeStr=_saveTypeStr;
                [self.navigationController pushViewController:detailViewController animated:YES];
            }
            
            if ([self.typeString isEqualToString:@"Continue"]) {
                
                BusinessCheckVouchContinueViewController *detailViewController = [[BusinessCheckVouchContinueViewController alloc] init];
                detailViewController.titleName=@"继续盘点";
                detailViewController.Whs=@"Continue";
                detailViewController.ckvId=self.ckvId;
                detailViewController.ckvNo=self.ckvNo;
                detailViewController.whsName=self.whsName;
                detailViewController.itemTypeName=self.itemTypeName;
                detailViewController.whsId=self.whsId;
                detailViewController.itemTypeCode=self.itemTypeCode;
                detailViewController.saveTypeStr=_saveTypeStr;
                [self.navigationController pushViewController:detailViewController animated:YES];
            }
            
            
            
        }

        
        
        
        
        
        
    }
}




-(void)closeKeyBoad
{
    [fengxiangview removeFromSuperview];
    [fengxiangScroll removeFromSuperview];
    [quxiao removeFromSuperview];
//    [tureBtn removeFromSuperview];
   [diview removeFromSuperview];
//    [scrollView removeFromSuperview];
//    [chaimageview removeFromSuperview];
    
    [erweima removeFromSuperview];
    [erweima1 removeFromSuperview];
}


-(void)shareBtnClick{

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"product" forKey:@"contentType"];
    [param setValue:_itemIdStr forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:@"Company/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            // NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                self.shuoming=[severDataDict.dataDict objectForKey:@"shareDescription"];
                self.shareUrl=[severDataDict.dataDict objectForKey:@"shareUrl"];
                
                fengxiangview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                fengxiangview.backgroundColor=[UIColor blackColor];
                fengxiangview.alpha=0.5;
                [self.view addSubview:fengxiangview];
                
                fengxiangScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,ScreenHeight-240, ScreenWidth,200)];
                fengxiangScroll.delegate = self;
                fengxiangScroll.userInteractionEnabled = YES;
                fengxiangScroll.scrollEnabled = YES;
                fengxiangScroll.showsVerticalScrollIndicator = FALSE;
                fengxiangScroll.showsHorizontalScrollIndicator = FALSE;
                fengxiangScroll.backgroundColor=[UIColor whiteColor];
                [self .view addSubview:fengxiangScroll];
                
                // 手势
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [fengxiangview addGestureRecognizer:singleFingerOne];
                
                
                
                
                NSArray*nameBtn=[[NSArray alloc]initWithObjects:@"发送給朋友",@"分享到朋友圈",nil];
                NSArray *arrayimage = @[@"faweixin",@"fenxiangpenyouquan"];
                for (int i = 0; i<2; i++) {
                    UIButton *nameButcenter = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   80*ScreenHeight/1334+15, ScreenWidth/3, 60)];
                    
                    [fengxiangScroll addSubview:nameButcenter];
                    
                    
                    UIButton *nameBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   nameButcenter.bottom+20, ScreenWidth/3, 14)];
                    [nameBut setTitle: [nameBtn objectAtIndex:i]  forState:UIControlStateNormal];
                    nameBut.tag=201+i;
                    [nameBut setTitleColor:shenhuicolor forState:0];
                    nameBut.titleLabel.font=[UIFont systemFontOfSize:14];
                    [nameBut addTarget:self action:@selector(fengxiang:) forControlEvents:UIControlEventTouchUpInside];
                    [fengxiangScroll addSubview:nameBut];
                    
                    
                    UIButton *wuyebuttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
                    wuyebuttonimage.frame = CGRectMake(ScreenWidth/3*i,  10, 60, 60);
                    
                    wuyebuttonimage.tag = 201+i;
                    wuyebuttonimage.center=nameButcenter.center;
                    [wuyebuttonimage setImage:[UIImage imageNamed: [arrayimage objectAtIndex:i]] forState:UIControlStateNormal];
                    [wuyebuttonimage addTarget:self action:@selector(fengxiang:) forControlEvents:UIControlEventTouchUpInside];
                    [fengxiangScroll addSubview:wuyebuttonimage];
                    
                    
                }
                
                
                quxiao = [[UIButton alloc] initWithFrame:CGRectMake(0,ScreenHeight-45,  ScreenWidth, 45)];
                [quxiao setTitle:@"取消" forState:UIControlStateNormal];
                quxiao.titleLabel.font=[UIFont systemFontOfSize:16];
                quxiao.backgroundColor=[UIColor whiteColor];
                [quxiao setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [quxiao addTarget:self action:@selector(closeKeyBoad) forControlEvents:UIControlEventTouchUpInside];
                [self .view addSubview:quxiao];
                UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth-30, 0.35)];
                line.backgroundColor=qianhuicolor;
                [quxiao addSubview:line];
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    

}






-(void)fengxiang:(UIButton*)sender{
    [fengxiangview removeFromSuperview];
    [fengxiangScroll removeFromSuperview];
    [quxiao removeFromSuperview];
    NSURL *url = [NSURL URLWithString:[[[commdity.Dict valueForKey:@"imageList"] objectAtIndex:0]objectForKey:@"imageUrlFull"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image1 = [[UIImage alloc] initWithData:data];
    if (sender.tag==201) {
        if (![[commdity.Dict objectForKey:@"details"]isEqualToString:@""]) {
            self.shuoming=[commdity.Dict objectForKey:@"details"];
        }
        [UMSocialData defaultData].extConfig.wechatSessionData.title =[commdity.Dict objectForKey:@"itemName"];
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:self.shareUrl];
        
        [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
        [UMSocialData defaultData].extConfig.wechatSessionData.url = self.shareUrl;
        [[UMSocialDataService defaultDataService]postSNSWithTypes:@[UMShareToWechatSession] content:self.shuoming image:image1 location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *response){
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
            
        }];
        
        
        
        
        
        
    }
    
    if (sender.tag==202) {
        
        //       [UMSocialData defaultData].extConfig.wechatSessionData.title =[commdity.Dict objectForKey:@"itemName"];
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:self.shareUrl];
        [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatTimeline].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.shareUrl;
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:[NSString stringWithFormat:@"%@",  [commdity.Dict objectForKey:@"itemName"]] image:image1 location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];
        
        
        
        
    }
    
}




@end
